<?php
declare(strict_types=1);

namespace App\Logic\User\Service\User;

use App\Library\SnowFlakeId;
use App\Library\WeChatClient;
use App\Logic\User\Repository\User\UserRepository;
use Illuminate\Support\Facades\Cache;

class LoginService
{
    public function serviceLogin(string $code, array $requestParams): array
    {
        $loginInfo = WeChatClient::client()->auth->session($code);
        if (!empty($loginInfo["openid"])) {
            $userService = new UserService();
            $userInfo    = $userService->serviceFind(["openid" => $loginInfo["openid"]]);
            $uid         = $userInfo["uid"] ?? SnowFlakeId::getId();
            $nickname    = $userInfo["nickname"] ?? "用户" . mt_rand(100, 20000);
            $score       = empty($userInfo["score"]) ? 300 : $userInfo["score"];
            $remark      = empty($userInfo["remark"]) ? "这家伙很懒 什么都没留下" : $userInfo["remark"];
            $avatar      = empty($userInfo["avatar_url"]) ? env("DEFAULT_USER_AVATAR") : $userInfo["avatar_url"];
            $profession  = empty($userInfo["profession"]) ? 0 : $userInfo["profession"];
            $tokenKey    = md5(SnowFlakeId::getId());
            if (empty($userInfo)) {
                // 查询默认专业配置
                $userProfession = (new UserProfessionService())->serviceFind(["is_default" => 1]);
                $createUser     = $userService->serviceCreate([
                    "uid"         => $uid,
                    "openid"      => $loginInfo["openid"],
                    "nickname"    => $nickname,
                    "score"       => 0.00,
                    "remark"      => $remark,
                    "avatar_url"  => $avatar,
                    "login_token" => $tokenKey,
                    "unionid"     => empty($loginInfo["unionid"]) ? "" : $requestParams["unionid"],
                    "profession"  => !empty($userProfession) ? $userProfession["uid"] : 0,
                    "channel_uid" => 0,//(int)$requestParams["channel_uid"] ?? 0,
                ]);
                if (!count($createUser)) return ["msg" => "信息记录失败"];
                UserScoreHistoryService::serviceUserScoreUpdate(300, "新用户注册", $uid);
            } else {
                (new UserRepository())->repositoryUpdate([["uid", "=", $uid]], ["login_token" => $tokenKey]);
            }
            $userCacheInfo = [
                "uid"        => $uid,
                "openid"     => $loginInfo["openid"],
                "nickname"   => $nickname,
                "mobile"     => empty($userInfo["mobile"]) ? "" : $userInfo["mobile"],
                "email"      => empty($userInfo["mobile"]) ? "" : $userInfo["mobile"],
                "avatar_url" => $avatar,
                "score"      => $score,
                "remark"     => $remark,
                "profession" => $profession,
            ];
            Cache::put("login:token:" . $tokenKey, json_encode($userCacheInfo), 86400 * 7);
            unset($userCacheInfo["mobile"], $userCacheInfo["openid"], $userCacheInfo["email"]);
            return ["token" => $tokenKey, "user" => $userCacheInfo];
        }
        return ["msg" => "登录失败"];
    }
}
